﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Text;
using System.Text.Json;
using static System.Net.Mime.MediaTypeNames;

// 构建请求 httpclient
var host = new HostBuilder()
    .ConfigureServices(services =>
    {
        services.AddHttpClient();
    })
.Build();
var _httpClientFactory = host.Services.GetRequiredService<IHttpClientFactory>();
var httpClient = _httpClientFactory.CreateClient();

// 请求地址
const string url = "http://32.9.111.20:8080/evss-front/evssapi?method=yhkjhztquery";

// 请求参数
var request = new Dictionary<string, string>
{
    {"cardno","" },
    {"name","" },
};

var bodyJson = new StringContent(
        JsonSerializer.Serialize(request),
        Encoding.UTF8,
        Application.Json);

// 发起请求
var response = await httpClient.PostAsync(url, bodyJson);

// 是否成功返回
if (!response.IsSuccessStatusCode)
    throw new HttpRequestException("response code error");

// 读取返回的body
using var contentStream = await response.Content.ReadAsStreamAsync();

// 转为定义的返回实体
var resEntity = JsonSerializer.DeserializeAsync<ResponseEntity>(contentStream);

Console.WriteLine(JsonSerializer.Serialize(resEntity));

Console.ReadKey();


public class ResponseEntity
{
    public string? Errorcode { get; set; }
    public string? Errormsg { get; set; }
    public string? Sfjh { get; set; }
    public string? Khrq { get; set; }
}
